package com.hdu.controller.authorityManagement;

import com.hdu.entity.Menu;
import com.hdu.entity.Role;
import com.hdu.pojo.Result;
import com.hdu.service.IMenuService;
import com.hdu.service.IRoleService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 权限实体类 前端控制器
 * </p>
 *
 * @author xiaobai
 * @since 2023-10-28
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "权限管理接口")
@RequestMapping("/role")
public class RoleController {

    private final IRoleService roleService;

    private final IMenuService menuService;

    @GetMapping
    public Result<?> getRoleInfo(String roleName) {
        List<Role> roleList = roleService.getRoleInfo("", roleName);
        log.info("查询角色信息：{}", roleList);
        return Result.success(roleList);
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable String id) {
        log.info("删除角色, id:{}", id);
        roleService.removeById(id);
        return Result.success();
    }

    @PostMapping
    public Result<?> append(@RequestBody Role role) {
        log.info("新增角色, menu: {}", role);
        roleService.append(role);
        return Result.success();
    }

    @PutMapping
    public Result<?> update(@RequestBody Role role) {
        log.info("更新角色信息 : {}", role);
        roleService.updateById(role);
        return Result.success();
    }

    @GetMapping("/menuList/{id}")
    public Result<?> getMenuInfo(@PathVariable String id) {
        String menuAssign = roleService.getById(id).getMenuList();
        List<Menu> menuList = menuService.getAllUserInfo(menuAssign);
        log.info("查询角色绑定菜单信息：{}", menuList);
        return Result.success(menuList);
    }

    @PostMapping("/assign")
    public Result<?> assign(String roleId, String[] menuId) {
        log.info("分配角色菜单信息 : {}", (Object[]) menuId);
        roleService.assign(roleId, menuId);
        return Result.success();
    }
}
